

function addSensorbitCod(Blockly) {







   //7219初始化
   Blockly.Python.microbit_matrix7219Init = function(block) {
    var dropdown_matrix_din = this.getFieldValue('matrix_din');
    var dropdown_matrix_cs = this.getFieldValue('matrix_cs');
    var dropdown_matrix_sda = this.getFieldValue('matrix_sda');
    var dropdown_matrix_intensity = this.getFieldValue('initIntensity');
    Blockly.Python.definitions_['import_microbit'] = 'from microbit import *';
    Blockly.Python.definitions_['import_matrix7219'] = 'from monitor import matrix7219';
    return 'matrix7219.init('+dropdown_matrix_din+ ', '+ dropdown_matrix_cs + ', '+ dropdown_matrix_sda+', '+dropdown_matrix_intensity +')\n';
  }
  
  //7219 点阵屏点显示亮度
  Blockly.Python.microbit_matrix7219SetPixel = function(block) {
    // var dropdown_matrixX = Blockly.Python.valueToCode(this, 'matrixX', Blockly.Python.ORDER_ATOMIC);
    // var dropdown_matrixY = Blockly.Python.valueToCode(this, 'matrixY', Blockly.Python.ORDER_ATOMIC);
    // var dropdown_intensity = Blockly.Python.valueToCode(this, 'intensity', Blockly.Python.ORDER_ATOMIC);
    var dropdown_matrixX = this.getFieldValue('matrixX');
    var dropdown_matrixY = this.getFieldValue('matrixY');
    var dropdown_intensity = this.getFieldValue('intensity');
    return 'matrix7219.set_pixel('+dropdown_matrixX+ ', '+ dropdown_matrixY + ', '+ dropdown_intensity +')\n';
  }

    //7219 点阵图像
  Blockly.Python.microbit_matrix7219Draw = function(block) {
      var choice = block.getFieldValue('lattice');
      if (choice.length <= 0) {
        choice = "0000000000000000000000000000000000000000000000000000000000000000";
      }else{
        choice = choice.substring(2,choice.length);
      }
      let result = Blockly.Python.hex_to_bin(choice);
    var choiceArrray = [];
    for (var i = 0; i < result.length / 8; i++) {
      choiceArrray.push(result.substring(i * 8, i * 8 + 8).replace(/1/g, '1'))
    }
    var image = '"' + choiceArrray[0]+ ':'+ choiceArrray[1]+ ':'+ choiceArrray[2]+ ':'+ choiceArrray[3]+ ':'+ choiceArrray[4]+ ':'+choiceArrray[5]+ ':'+choiceArrray[6]+ ':'+choiceArrray[7]+ '"';
    return 'matrix7219.draw('+ image +')\n';
  }


    Blockly.Python.hex_to_bin= function (str) {
      let hex_array = [{key:0,val:"0000"},{key:1,val:"0001"},{key:2,val:"0010"},{key:3,val:"0011"},{key:4,val:"0100"},{key:5,val:"0101"},{key:6,val:"0110"},{key:7,val:"0111"},
      {key:8,val:"1000"},{key:9,val:"1001"},{key:'a',val:"1010"},{key:'b',val:"1011"},{key:'c',val:"1100"},{key:'d',val:"1101"},{key:'e',val:"1110"},{key:'f',val:"1111"}]
      let value=""
      for(let i=0;i<str.length;i++){
          for(let j=0;j<hex_array.length;j++){
              if(str.charAt(i).toLowerCase()== hex_array[j].key){
                  value = value.concat(hex_array[j].val)
                  break
              }
          }
      }
      return value
    };



    return Blockly;
}

module.exports = addSensorbitCod;

